package com.yinda.dt.produce.module.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.yinda.dt.produce.R;
import com.yinda.dt.produce.app.DemoActivity;

public class NewWaitOrderListActivity extends DemoActivity implements View.OnClickListener {

    private TabLayout tl_title;
    private ViewPager vp_content;

    private PagerAdapter adapter;

    private int type;
    private String approvalCode;

    private LinearLayout ll_search;
    private LinearLayout ll_left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_wait_order_list);

        type = getIntent().getIntExtra("type", 1);
        approvalCode = getIntent().getStringExtra("approvalCode");

        tl_title = (TabLayout) findViewById(R.id.tl_title);
        vp_content = (ViewPager) findViewById(R.id.vp_content);
        adapter = new NewWaitOrderAdapter();
        vp_content.setAdapter(adapter);
        tl_title.setupWithViewPager(vp_content);

        ll_search = (LinearLayout) findViewById(R.id.ll_search);
        ll_search.setOnClickListener(this);

        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_left.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_search) {
            Intent intent = new Intent(this, SearchActivity.class);
            if (vp_content.getCurrentItem() == 0) {
                intent.putExtra("status", 1);
            } else {
                intent.putExtra("status", 3);
            }
            intent.putExtra("approvalCode", approvalCode);
            startActivity(intent);
        }
        if (id == R.id.ll_left) {
            finish();
        }

    }

    private class NewWaitOrderAdapter extends FragmentPagerAdapter {

        Fragment page1;
        Fragment page2;

        public NewWaitOrderAdapter() {
            super(getSupportFragmentManager());

            page1 = new DealListFragment();
            page2 = new DealListFragment();

            Bundle bundle1 = new Bundle();
            Bundle bundle2 = new Bundle();

            bundle1.putInt("status", 1);
            bundle2.putInt("status", 3);
            bundle1.putString("approvalCode", approvalCode);
            bundle2.putString("approvalCode", approvalCode);
            page1.setArguments(bundle1);
            page2.setArguments(bundle2);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return page1;
            }
            if (position == 1) {
                return page2;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "未处理";
            }
            if (position == 1) {
                if (type == 1) {
                    return "正在处理";
                }
                if (type == 2) {
                    return "正在生产";
                }
            }
            return "";
        }
    }

}
